package me.siphone.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bin.david.form.core.SmartTable;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.style.FontStyle;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import java.util.ArrayList;
import java.util.List;
import me.siphone.http.api.QueryDeviceApi;
import me.siphone.http.model.HttpData;
import me.siphone.model.Device;
import org.linphone.R;

public class DeviceListFragment extends BaseFragment {

    private SmartTable table;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device_list, container, false);

        table = view.findViewById(R.id.device_table);

        TableConfig config = table.getConfig();
        config.setShowTableTitle(false);
        config.setShowXSequence(false);
        config.setShowYSequence(false);
        config.setColumnTitleStyle(new FontStyle(getContext(), 18, Color.BLACK));
        config.setContentStyle(new FontStyle(getContext(), 16, Color.GRAY));
        config.setVerticalPadding(30);
        config.setColumnTitleVerticalPadding(20);

        queryDeviceList();

        return view;
    }

    private void queryDeviceList() {
        EasyHttp.get(this)
                .api(new QueryDeviceApi())
                .request(
                        new HttpCallbackProxy<HttpData<List<QueryDeviceApi.Device>>>(this) {
                            @Override
                            public void onHttpSuccess(
                                    @NonNull HttpData<List<QueryDeviceApi.Device>> result) {
                                super.onHttpSuccess(result);
                                if (result.getRows() != null) {
                                    List<Device> devices = new ArrayList<>();
                                    for (QueryDeviceApi.Device row : result.getRows()) {
                                        devices.add(
                                                new Device(
                                                        row.getDeviceName(),
                                                        row.getDeviceUid(),
                                                        convert2DeviceType(row.getDeviceType()),
                                                        convert2OnlineStatus(row.getOnlineStatus()),
                                                        row.getDeviceInstallationAddr(),
                                                        row.getDeviceOwner()));
                                    }
                                    table.setData(devices);
                                }
                            }
                        });
    }

    private String convert2DeviceType(int type) {
        switch (type) {
            case 1:
                return "终端设备";
            case 2:
                return "软电话";
            case 3:
                return "村级设备";
            default:
                return "";
        }
    }

    private String convert2OnlineStatus(int status) {
        switch (status) {
            case 0:
                return "离线";
            case 1:
                return "在线";
            default:
                return "";
        }
    }
}
